package com.hzit.manager.controller;

import com.hzit.manager.exception.MyRuntimeException;
import com.hzit.manager.service.SentineService;
import com.ruoyi.common.core.domain.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@RestController
@RequestMapping("/sentinel")
public class SentinelController {

	@Autowired
	private SentineService sentineService;

	//1. 异常处理时定义的资源
	@GetMapping("/res1")
	public R res1(){
		String rs = sentineService.myRes1();
		return R.ok(rs);
	}
	@GetMapping("/res11")
	public R res11(){
		String rs = sentineService.myRes1();
		return R.ok(rs);
	}
	@GetMapping("/res12")
	public R res12() throws InterruptedException {
		TimeUnit.SECONDS.sleep(1);
		return R.ok();
	}
	@GetMapping("/res13")
	public R res13(int id) throws InterruptedException {
		if(id < 0){
			throw new MyRuntimeException("出现了异常（异常比例）！");
		}
		return R.ok();
	}
	@GetMapping("/res14")
	public R res14(int id) throws InterruptedException {
		if(id < 0){
			throw new MyRuntimeException("出现了异常(异常数)！");
		}
		return R.ok();
	}
	//2. 方式三：返回布尔值方式定义资源
	@GetMapping("/res2")
	public R res2(){
		String rs = sentineService.myRes2();
		return R.ok(rs);
	}

	//3. 使用@SentinelResource注解义资源,使用blockHandler处理资源
	// 官网：https://github.com/alibaba/Sentinel/wiki/%E6%B3%A8%E8%A7%A3%E6%94%AF%E6%8C%81
	@GetMapping("/res3")
	public R res3(){
		String rs = sentineService.myRes3();
		return R.ok(rs);
	}
	//4. 使用@SentinelResource注解义资源,使用blockHandlerClass处理资源
	@GetMapping("/res4")
	public R res4(){
		String rs = sentineService.myRes4();
		return R.ok(rs);
	}
	//5. 使用@SentinelResource注解义资源,使用fallback处理资源
	@GetMapping("/res5")
	public R res5(){
		String rs = sentineService.myRes5();
		return R.ok(rs);
	}
	//6. 使用@SentinelResource注解义资源,使用fallbackClass处理资源
	@GetMapping("/res6")
	public R res6(){
		String rs = sentineService.myRes6();
		return R.ok(rs);
	}
}
